package com.kaka.bus.engine.prompt;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.widget.ImageView;
import android.widget.TextView;

import com.kaka.bus.R;
import com.kaka.bus.adapter.PromptAdapter;
import com.kaka.bus.base.BaseActivity;
import com.kaka.bus.bean.DrainProductBean;
import com.kaka.bus.bean.HttpWrapper;
import com.kaka.bus.constant.Constant;
import com.kaka.bus.engine.amount.TopProduct.TopProductActivity;
import com.kaka.bus.view.MutipleLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class PromptActivity extends BaseActivity<PromptPresenter> implements PromptContract.View, PromptAdapter.PromptClick {

    public static final int BLACK = 1;
    public static final int REFURES = 2;
    public static final String STATUTYPE = "STATUTYPE";

    @BindView(R.id.prompt_recycle)
    RecyclerView prompt_recycle;
    @BindView(R.id.img_prompt)
    ImageView img_prompt;

    @BindView(R.id.mutiple)
    MutipleLayout mutiple;
    @BindView(R.id.toolbar_back)
    ImageView toolbar_back;
    @BindView(R.id.toolbar_name)
    TextView toolbar_name;
    @BindView(R.id.tv_content)
    TextView tv_content;
    private int type;
    String content;
    private List<DrainProductBean> drainProductBeans = new ArrayList<>();
    PromptAdapter mAdapter;

    @Override
    protected void initView() {
        super.initView();
        type = getIntent().getIntExtra(STATUTYPE, 0);
        if (type == REFURES) {
            content = "抱歉! 由于您的信用等级较低，\n暂不符合我们的借款要求，过段时间再来吧";
            img_prompt.setImageResource(R.drawable.icon_dengjidi_type2);
        } else {
            content = "抱歉! 由于您的信用等级较低，\n不符合我们的借款要求";
            img_prompt.setImageResource(R.drawable.icon_dengjidi_type1);
        }
        prompt_recycle.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mAdapter = new PromptAdapter(this, drainProductBeans, this);
        prompt_recycle.setAdapter(mAdapter);
        SpannableString spannableString = new SpannableString(content);
        spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#EE4242")), content.indexOf("抱歉!"), content.indexOf("抱歉!") + 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv_content.setText(spannableString);


        mutiple.setEmptyText("暂无数据");
        mutiple.setEmptyImage(R.drawable.empty_view_kongtai);
        mutiple.setStatus(MutipleLayout.Loading);
        toolbar_name.setText("提示");

    }

    @Override
    protected void initData() {
        super.initData();
        mPresenter.getTailProduct();
    }

    @Override
    protected void initListener() {
        toolbar_back.setOnClickListener(v -> finish());
        mutiple.setOnReloadListener(v -> {
            mPresenter.getTailProduct();
        });
    }

    @Override
    public void itemClickListemer(DrainProductBean productBean) {
        Intent intent = new Intent(this, TopProductActivity.class);
        intent.putExtra(TopProductActivity.PRODUCTBEAN, productBean.getProduct());
        intent.putExtra(TopProductActivity.PRODUCTID, productBean.getProductId());
        intent.putExtra(TopProductActivity.BANNERID, productBean.getLocationId());
        startActivity(intent);
    }

    @Override
    public void getTailProductSuccess(HttpWrapper<List<DrainProductBean>> listHttpWrapper) {
        mutiple.setStatus(MutipleLayout.Success);
        if (listHttpWrapper.code.equals(Constant.SUCCESS_CODE)) {
            drainProductBeans.clear();
            drainProductBeans.addAll(listHttpWrapper.getData());
            mAdapter.notifyDataSetChanged();
        } else {
            showMessage(listHttpWrapper.info);
        }
    }

    @Override
    public void getTailProductfail() {
        mutiple.setStatus(MutipleLayout.Error);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        drainProductBeans.clear();
    }

    @Override
    protected boolean needFullScreen() {
        return true;
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_prompt;
    }


}
